import SparkMD5 from "spark-md5";
onmessage = (e) => {
  const { chunks } = e.data;
  const spark = new SparkMD5();
  function _read(i: number) {
    if (i > chunks.length - 1) {
      postMessage({
        hash: spark.end(),
      });
      return false;
    }
    const blob = chunks[i];
    const reader = new FileReader();
    reader.onload = function (e) {
      const bytes = e.target?.result as string;
      spark.append(bytes!);
      _read(i + 1);
    };
    reader.readAsArrayBuffer(blob);
  }
  _read(0);
};
